package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.mapper.OwnerMapper;
import com.woniuxy.mapper.ParkingMapper;
import com.woniuxy.model.Owner;
import com.woniuxy.model.Parking;
import com.woniuxy.service.ParkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ParkingServiceImpl implements ParkingService {
    @Autowired
    private ParkingMapper parkingMapper;

    @Autowired
    private OwnerMapper ownerMapper;
    /*进行新增
    * */
    @Override
    public void addParking(Parking parking) {
        if(parking.getOwnerId()==null)
            throw new RuntimeException("错误的信息,owner不能为空");
        Owner owner = ownerMapper.selectById(parking.getOwnerId());
        if(owner==null)
            throw new RuntimeException("不存在这样的业主");
        parkingMapper.insert(parking);
    }

    @Override
    public void deleteBatch(Integer id) {
        parkingMapper.deleteById(id);
    }

    @Override
    public void updateById(Parking parking) {
        //判断传入的业主id是否存在
        Owner owner = ownerMapper.selectById(parking.getOwnerId());
        if(owner==null){
            throw new RuntimeException("要修改的ownerid不存在");
        }
        parkingMapper.updateById(parking);
    }

    @Override
    public List<Parking> pageQuery(Integer curentpage, Integer size) {
        QueryWrapper<Parking> parkingQueryWrapper = new QueryWrapper<>();
        IPage<Parking> page = new Page<>(curentpage,size);
        List<Parking> parkings = parkingMapper.selectList(page, parkingQueryWrapper);
        for(Parking parking:parkings){
            Integer ownerId = parking.getOwnerId();
            Owner owner = ownerMapper.selectById(ownerId);
            parking.setOwner(owner);
        }
        return parkings;
    }
}
